package map_set.map.hash;

import java.util.Arrays;
import java.util.Objects;

public class HashBuck2<K, V> {
    static class Node<K, V> {
        public K key;
        public V val;
        public Node<K, V> next;

        public Node(K key, V val) {
            this.key = key;
            this.val = val;
        }
    }
    public Node<K, V>[] table = (Node<K, V>[]) new Node[8];
    public int size;

    public static final double DEFAULT_LOAD_FACTOR = 0.75f;

    public void push(K key, V val) {
        int hashCode = key.hashCode();
        int index = hashCode % table.length;

        Node<K, V> cur = table[index];
        while (cur != null) {
            if (cur.key.equals(key)) {
                cur.val = val;
                return;
            }
            cur = cur.next;
        }
        Node<K, V> node = new Node<>(key, val);
        node.next = table[index];
        table[index] = node;
        size++;
    }

    public V getVal(K key) {
        int hashCode = key.hashCode();
        int index = hashCode % table.length;
        Node<K, V> cur = table[index];
        while (cur != null) {
            if (cur.key.equals(key)) {
                return cur.val;
            }
        }
        return null;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        HashBuck2<?, ?> hashBuck2 = (HashBuck2<?, ?>) o;
        return size == hashBuck2.size && Objects.deepEquals(table, hashBuck2.table);
    }

    @Override
    public int hashCode() {
        return Objects.hash(Arrays.hashCode(table), size);
    }
}
